package com.gz.steak.common.interceptor;
import com.gz.steak.common.exception.BusinessException;
import com.gz.steak.common.exception.EmBusinessError;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 拦截器
 * @Author 刘牌
 * @Date 2020/3/27 0027 0:40
 * @Version 1.0
 */
@Component
public class TokenInterceptor implements HandlerInterceptor {
    private final static Logger logger = LoggerFactory.getLogger(HandlerInterceptorAdapter.class);
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        /**
         * 登陆请求和验证码请求不拦截
         */
        if (request.getRequestURI().equals("/user/captcha.jpg") || request.getRequestURI().equals("/user/login")){
            return true;
        }
        else {
            /**
             * token为空代表没有登陆，不能访问，因为每次访问接口都要带上token
             */
            if (request.getHeader("token") == null){
                throw new BusinessException(EmBusinessError.NO_LOGIN);
            }else {
                return true;
            }
        }
    }
}
